﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmInvite : FrmBase
    {
        private Size m_qrcodeImageSize = new Size(240, 240);
        private List<string> m_inviteDomains = new List<string>();

        public FrmInvite()
        {
            InitializeComponent();
            ClearControls();
        }

        private void ClearControls()
        {
            this.lblInviteCode.Text = string.Empty;
            this.linkLabelInviteUrl.Text = "无";
            UiUtil.ClearPictureBox(this.pictureBox1);
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            if (!base.CheckLogin())
                return;

            ClearControls();

            try
            {
                var inviteCode = base.MemberInfo.InviteCode;
                m_inviteDomains = QttHelper.GetShareDomain(base.MemeberToken);
                Random r = new Random();
                int domainIndex = r.Next(0, m_inviteDomains.Count - 1);
                var inviteUrl = string.Format("http://{0}?id={1}", m_inviteDomains[domainIndex], inviteCode);

                this.lblInviteCode.Text = inviteCode;
                SwitchInviteUrl(inviteUrl);
            }
            catch (Exception ex)
            {
                this.pictureBox1.Tag = null;
                Console.WriteLine(ex.Message);
            }
        }

        private void SwitchInviteUrl(string inviteUrl)
        {
            var inviteImage = QttHelper.GetInviteImage(inviteUrl, m_qrcodeImageSize);
            var inviteImageDefault = QttHelper.GetInviteImage(inviteUrl, QttHelper.DefaultQRCodeImageSize);

            this.Invoke(new Action(() =>
            {
                this.linkLabelInviteUrl.Text = inviteUrl;
                this.pictureBox1.Image = inviteImage;
                this.pictureBox1.Size = inviteImage.Size;
                this.pictureBox1.Tag = inviteImageDefault;
            }));
        }

        private void linkLabelInviteUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.linkLabelInviteUrl.Text.StartsWith("http"))
            {
                Process.Start(this.linkLabelInviteUrl.Text);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.pictureBox1.Tag != null)
            {
                var qrcodeImage = this.pictureBox1.Tag as Image;
                ContextMenuStrip cms = new ContextMenuStrip();
                cms.Items.Add("另存二维码", null, (o, args) =>
                {
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        qrcodeImage.Save(this.saveFileDialog1.FileName);
                    }
                });
                cms.Items.Add("另存分享图片", null, (o, args) =>
                {
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        var bgImage = QttHelper.GetImageByUrl(QttHelper.InviteBgImageUrl);
                        using (var g = Graphics.FromImage(bgImage))
                        {
                            g.DrawImage(qrcodeImage, new Rectangle(248, 757, qrcodeImage.Width, qrcodeImage.Height));
                        }
                        bgImage.Save(this.saveFileDialog1.FileName);
                    }
                });
                cms.Show(this.pictureBox1, e.Location);
            }
        }

        private void lblInviteCode_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(this.lblInviteCode.Text);
                MsgBox.ShowInfo("已复制！");
            }
            catch (Exception ex)
            {
            }
        }

        private void btnSwitchInviteUrl_Click(object sender, EventArgs e)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            foreach (var url in m_inviteDomains)
            {
                var inviteUrl = string.Format("http://{0}?id={1}", url, base.MemberInfo.InviteCode);
                cms.Items.Add(inviteUrl, null, (o, args) =>
                {
                    SwitchInviteUrl((o as ToolStripItem).Text);
                });
            }
            cms.Show(this.btnSwitchInviteUrl, new Point(0,this.btnSwitchInviteUrl.Height));
        }
    }
}
